<template>
  <div style="padding: 20px"
       v-wechat-title="'操作日志'">
    <el-form
        ref="queryForm"
        :model="queryParams"
        size="small"
        :inline="true"
    >
      <el-form-item>
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple || isAuth('sys:operation:delete')" @click="handleDelete">删除</el-button>
      </el-form-item>
      <el-form-item>
        <el-input
            clearable
            class="filter-item"
            v-model="queryParams.username"
            style="width: 150px;"
            placeholder="操作人账号"
            @keyup.enter.native="searchByParam()">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-input
            clearable
            class="filter-item"
            v-model="queryParams.methodClass"
            style="width: 150px;"
            placeholder="接口类"
            @keyup.enter.native="searchByParam()">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryParams.microService"
                   clearable
                   style="width: 150px;"
                   placeholder="微服务模块">
          <el-option
              v-for="item in microDict"
              :key="item.value"
              :label="item.value"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-select v-model="queryParams.businessType"
                   clearable
                   style="width: 150px;"
                   placeholder="操作类型">
          <el-option
              v-for="item in businessTypeData"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryParams.status"
                   clearable
                   style="width: 150px;"
                   placeholder="操作状态">
          <el-option
              v-for="item in statusData"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item>
        <el-select v-model="queryParams.requestMethod"
                   clearable
                   style="width: 150px;"
                   placeholder="请求方式">
          <el-option
              v-for="item in requestMethodData"
              :key="item.value"
              :label="item.value"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
            v-model="operationTime"
            type="datetimerange"
            :picker-options="pickerOptions"
            style="width: 210px"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            :default-time="['12:00:00']"
            value-format="yyyy-MM-dd HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="operationTableData"
        stripe
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="操作人昵称:">
              <span>{{ props.row.nickname }}</span>
            </el-form-item>
            <el-form-item label="接口类:">
              <span>{{ props.row.methodClass }}</span>
            </el-form-item>
            <el-form-item label="请求参数:">
              <span>{{ props.row.operationParam ? props.row.operationParam : '无' }}</span>
            </el-form-item>
            <el-form-item label="返回参数:">
              <span>{{ props.row.jsonResult ? props.row.jsonResult : '无' }}</span>
            </el-form-item>
            <el-form-item label="错误消息:">
              <span>{{ props.row.errorMsg ? props.row.errorMsg : '无' }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column
          type="selection">
      </el-table-column>
      <el-table-column
          prop="microService"
          label="微服务模块">
      </el-table-column>
      <el-table-column
          prop="username"
          label="操作人账号">
      </el-table-column>
      <el-table-column
          prop="interfaceDepict"
          label="接口描述">
      </el-table-column>
      <el-table-column
          prop="businessType"
          :formatter="businessTypeFormat"
          label="业务类型">
      </el-table-column>
      <el-table-column
          prop="requestMethod"
          label="请求方式">
      </el-table-column>
      <el-table-column
          prop="requestUrl"
          label="URL">
      </el-table-column>
      <el-table-column
          prop="operatorIp"
          label="IP">
      </el-table-column>
      <el-table-column
          prop="operatorLocation"
          label="操作地点">
      </el-table-column>
      <el-table-column
          label="操作状态">
        <template slot-scope="scope">{{ scope.row.status == 0 ? '成功' : '失败' }}</template>
      </el-table-column>
      <el-table-column
          prop="operationTime"
          label="操作时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <el-button type="danger"
                       icon="el-icon-delete"
                       circle
                       :disabled="isAuth('sys:operation:delete')"
                       @click="handleDelete(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="pagination.total>0"
        :limit.sync="pagination.pageSize"
        :page.sync="pagination.currentPage"
        :total="pagination.total"
        :page-sizes="[5, 10, 20, 50]"
        @pagination="handleQuery"
    />
  </div>

</template>

<script>
import {getOperationList, deleteOperation} from '@/api/log/operation'
import {listDictItemByCode} from "@/api/system/dict/dict-item";

export default {
  name: "operation",
  filters: {
    ellipsis(value) {
      if (!value) return ''
      if (value.length > 250) {
        value = value.slice(0, 250) + '...'
      }
      return value;
    },
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      operationTime: [],
      operationTableData: [],
      businessTypeData: [
        {
          value: '0',
          label: '其它'
        },
        {
          value: '1',
          label: '查询'
        },
        {
          value: '2',
          label: '新增'
        },
        {
          value: '3',
          label: '修改'
        },
        {
          value: '4',
          label: '删除'
        },
        {
          value: '5',
          label: '批量删除'
        },
        {
          value: '6',
          label: '授权'
        },
        {
          value: '7',
          label: '导出'
        },
        {
          value: '8',
          label: '导入'
        }
      ],
      requestMethodData:[
        {
          value: 'GET'
        },
        {
          value: 'POST'
        },
        {
          value: 'UPDATE'
        },
        {
          value: 'DELETE'
        },
      ],
      statusData: [{
        value: '0',
        label: '成功'
      }, {
        value: '1',
        label: '失败'
      }],
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      queryParams: {
        currentPage: 1,
        pageSize: 10,
        username: '',
        methodClass: '',
        businessType: '',
        status: '',
        requestMethod: '',
        microService:''
      },
      // 选中数组
      ids:[],
      // 非多个禁用
      multiple: true,
      microDict:[]
    }
  },
  methods: {
    handleQuery() {
      this.queryParams.currentPage = this.pagination.currentPage
      this.queryParams.pageSize = this.pagination.pageSize
      if (this.operationTime) {
        this.queryParams.startTime = this.operationTime[0]
        this.queryParams.endTime = this.operationTime[1]
      }else {
        this.queryParams.startTime = ''
        this.queryParams.endTime = ''
      }
      getOperationList(this.queryParams).then(res => {
        this.operationTableData = res.data.records
        this.pagination.total = res.data.total
      })
    },
    //翻译操作类型
    businessTypeFormat(row) {
      const type = row.businessType;
      const filter = this.businessTypeData.filter(item => item.value == type);
      return filter[0].label
    },
    searchByParam() {
      if (this.timeKeyWord) {
        this.keyword.startTime = this.timeKeyWord[0]
        this.keyword.endTime = this.timeKeyWord[1]
      } else {
        this.keyword.startTime = ''
        this.keyword.endTime = ''
      }
      this.handleQuery(this.currentPage, this.pageSize, this.keyword)
    },
    // 多选
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    //重置
    handleReset() {
      this.pagination = {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
      this.queryParams = {
      }
      this.operationTime = []
      this.handleQuery()
    },
    handleMicroDict() {
      listDictItemByCode('micro_service').then(response => {
        this.microDict = response.data
      })
    },
    handleDelete(row) {
      const ids = [row.id || this.ids].join(',')
      this.$confirm('确认删除已选中的数据项？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteOperation(ids).then( res => {
          if (res.status) {
            this.$message.success('刪除成功')
            this.handleQuery()
          }
        })
      }).catch(() => {
      })
    },
  },
  created() {
    this.handleQuery()
    this.handleMicroDict()
  }
}
</script>

<style scoped>
.demo-table-expand {
  margin-left: 106px;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
